package com.chainyoung.trademarket.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chainyoung.common.base.BaseFragment;
import com.chainyoung.common.common_widget.loadsir.callback.Callback;
import com.chainyoung.common.common_widget.loadsir.core.LoadService;
import com.chainyoung.common.common_widget.loadsir.core.LoadSir;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.custom_widget.EmptyCallback;
import com.chainyoung.common.custom_widget.LoadingCallback;
import com.chainyoung.common.custom_widget.RetryCallback;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.component_tmmarket.ui.SearchAppActivity;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;
import com.chainyoung.trademarket.R;
import com.chainyoung.trademarket.adapter.FindAdapter;
import com.chainyoung.trademarket.di.component.DaggerMainFragmentComponent;
import com.chainyoung.trademarket.di.component.MainFragmentComponent;
import com.chainyoung.trademarket.di.module.FindData;
import com.chainyoung.trademarket.di.module.MainFragmentModule;
import com.chainyoung.trademarket.entity.FindItem;
import com.chainyoung.trademarket.mvp.contract.FindContract;
import com.chainyoung.trademarket.mvp.presenter.FindPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

@Route(path = RouterConstants.PATH_FIND)
public class FindFragment extends BaseFragment<FindPresenter> implements FindContract.View {


    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;

    private FindAdapter mAdapter;
    private List<FindItem> mBannerList = new ArrayList<>();
    private List<FindItem> mList = new ArrayList<>();
    private LoadService loadService;
    private FindData findData;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMainFragmentComponent.builder()
                .appComponent(appComponent)
                .mainFragmentModule(new MainFragmentModule(this))
                .build()
                .inject(this);
    }


    @Override
    protected void initUi() {
        super.initUi();
        setCurrentTitle(R.string.find);
        showAndSetBarRightIcon(R.mipmap.icon_search);
        initRecyclerView();
        initLoadSir();
    }


    private void initRecyclerView() {
        mAdapter = new FindAdapter(mContext, mBannerList, mList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter);
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                loadData();
            }
        });
    }

    private void initLoadSir() {
        LoadSir loadSir = new LoadSir.Builder()
                .addCallback(new LoadingCallback())
                .addCallback(new EmptyCallback())
                .addCallback(new RetryCallback())
                .setDefaultCallback(LoadingCallback.class)
                .build();
        loadService = loadSir.register(refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                loadData();
            }
        });
    }

    @Override
    public void onRightImgClicked(View v) {
        super.onRightImgClicked(v);
        RouterUtils.getInstance()
                .build(RouterConstants.PATH_OTHERAPP)
                .withString("title", LibConstant.BROWSER_INDEX_URL)
                .withString(URL, LibConstant.BROWSER_INDEX_URL)
                .navigation(mContext);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onFirstUserVisible() {
        loadData();
    }

    @Override
    public void onUserInvisible() {
        super.onUserInvisible();
        if (findData == null) {
            loadData();
        }


    }

    private void loadData() {
        mPresenter.getFindData();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_find;
    }


    @Override
    public void onGetDataSuccess(FindData data) {
        refreshLayout.finishRefresh();
        findData = data;
        if (data == null) {
            loadService.showCallback(EmptyCallback.class);
            return;
        }
        mBannerList.clear();
        mList.clear();
        mBannerList.addAll(data.getBanners());
        mList.addAll(data.getList());
        mAdapter.notifyDataSetChanged();
        loadService.showSuccess();
    }

    @Override
    public void onGetDataFailed() {
        refreshLayout.finishRefresh();
        loadService.showCallback(RetryCallback.class);
    }


}
